package app.widget;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Scroller;
import android.widget.TextView;

public class ScrollerMenu extends FrameLayout {

	private ThumbContainer thumbContainer;

	private boolean isLayout = false;

	private Handler handler = new Handler();

	private TextView thumb;

	private int menuCount;
	
	private int screemWidth = 0;

	/**
	 * 上次选中的菜单项
	 */
	private int lastSelectPosition = -1;

	private boolean isIndicater = false;

	/**
	 * 菜单选择监听器
	 */
	private OnMenuItemSelectListener onMenuItemSelectListener;

	/**
	 * 菜单项的宽度
	 */
	private int thumbWidth;

	private Drawable thumbDrawable;

	public ScrollerMenu(Context context) {
		super(context);
		init();
	}

	public ScrollerMenu(Context context, AttributeSet attrs) {
		super(context, attrs);
		int thumbRes = attrs.getAttributeResourceValue(null, "thumb", 0);
		thumbDrawable = getContext().getResources().getDrawable(thumbRes);

		init();
	}

	private void init() {
		if (!isLayout) {
			thumbContainer = new ThumbContainer(getContext());
			addView(thumbContainer, new LayoutParams(LayoutParams.FILL_PARENT,
					LayoutParams.FILL_PARENT));

			requestLayout();
			isLayout = true;
		}
		
	}

	@Override
	protected void onLayout(boolean changed, int left, int top, int right,
			int bottom) {
		super.onLayout(changed, left, top, right, bottom);

		screemWidth = getWidth();
		
		//默认选择 第一项
		select(0, false, menuCount);
		
		/**
		 * 添加Thumb
		 */
		handler.post(new Runnable() {
			public void run() {
			}
		});
	}

	/**
	 * /** 菜单选择监听器
	 * 
	 * @param onMenuItemSelectListener
	 */
	public void setOnMenuItemSelectListener(
			OnMenuItemSelectListener onMenuItemSelectListener) {
		this.onMenuItemSelectListener = onMenuItemSelectListener;
	}

	/**
	 * 通过代码选择菜单
	 * 
	 * @param position
	 *            选择的位置
	 * @param isIndicater
	 *            true: 操作不会触发OnMenuItemSelect事件, false
	 *            操作会触发onMenuItemSelectListener
	 */
	public void select(final int position, boolean isIndicater,
			final int menuCount) {
		
		this.menuCount = menuCount;
		
		this.isIndicater = isIndicater;
		handler.postDelayed(new Runnable() {
			public void run() {
				thumbContainer.select(position, menuCount);
			}
		}, 0);
	}

	/**
	 * 隐藏滑动条
	 */
	public void hideThumb() {
		if (thumb != null) {
			if (thumb.getVisibility() != View.INVISIBLE) {
				thumb.setVisibility(View.INVISIBLE);
				lastSelectPosition = -1;
			}
		}
	}

	/**
	 * 显示滑动条
	 * */
	public void showThumb() {
		if (thumb != null) {
			if (thumb.getVisibility() != View.VISIBLE) {
				thumb.setVisibility(View.VISIBLE);
			}
		}
	}

	public void setThumb(Drawable drawable) {
		thumbDrawable = drawable;
		thumb.setBackgroundDrawable(thumbDrawable);
	}

	public void setThumb(int resId) {
		setThumb(getContext().getResources().getDrawable(resId));
	}

	/**
	 * 滑动菜单项被选中时的回调方法
	 */
	public interface OnMenuItemSelectListener {
		public void OnMenuItemSelect(int menuPosition);
	}

	/**
	 * 菜单的滑动拖拽条
	 */
	private class ThumbContainer extends LinearLayout {

		private Scroller mScroller;

		public ThumbContainer(Context context) {
			super(context);
			init();
		}

		public void computeScroll() {
			super.computeScroll();
			if (mScroller.computeScrollOffset()) {
				scrollTo(mScroller.getCurrX(), 0);
				postInvalidate();
			}
		}

		private void init() {
			mScroller = new Scroller(getContext());

			thumb = new TextView(getContext());
			if (thumbDrawable != null) {
				thumb.setBackgroundDrawable(thumbDrawable);
				thumbWidth = ((BitmapDrawable) thumbDrawable).getBitmap()
						.getWidth();
			} else {
				thumb.setBackgroundColor(0x44f0f0f0);
			}
			thumb.setVisibility(View.VISIBLE);

			// TODO 设置 thum 大小
			addView(thumb, new LayoutParams(LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT));

			invalidate();
			
			forceLayout();
			
		}

		void select(int position, int menuCount) {

			// /* 右边不能出界 */
			if (position >= menuCount) {
				position = menuCount - 1;
			}

			/* 左边不能出界 */
			if (position < 0) {
				position = 0;
			}

			int scrollX = getScrollX();
			int dx = 0;

			int eachMenuWidth = screemWidth / menuCount;

//			PABankLog.i("zbkc==", "  screemWidth==" + screemWidth + "  eachMenuWidth=="
//					+ eachMenuWidth + "  menuCount==" + menuCount);

			int left = eachMenuWidth * position + (eachMenuWidth - thumbWidth)
					/ 2;

			// // 滑动 超出屏幕
			if (scrollX <= 0) {
				dx = Math.abs(getScrollX()) - left;
			} else {
				dx = -(Math.abs(getScrollX()) - left);
			}

			mScroller.startScroll(getScrollX(), 0, dx, 0, 500);
			invalidate();

			if (position != lastSelectPosition) {
				lastSelectPosition = position;

				if (!isIndicater) {
					if (onMenuItemSelectListener != null) {
						final int tempP = position;
						handler.postDelayed(new Runnable() {
							public void run() {
								onMenuItemSelectListener
										.OnMenuItemSelect(tempP);
							}
						}, 0);
					}
				}
			}
		}
	}

}
